package org.marverse.universe.properties

import scala.collection.Map
import scala.collection.mutable.HashMap
import util.{MutableTypedMap, TypedKey}
import persistance.PId

class Properties(universe: Universe) extends UniverseExtension {
  
  override def check(implicit ctx: MutableTypedMap[TypedKey, Any]) {
    persistentProperties.keys foreach { _.check }
    functionProperties.keys foreach { _.check }
  }
  
  private val pp = new HashMap[PId, PersistentProperty.State]()
  lazy val persistentProperties: Map[PersistentProperty, PersistentProperty.State] =
    new Map[PersistentProperty, PersistentProperty.State] {
      def get(key: PersistentProperty) = pp.get(key.pid)
      def size = pp.size
      def elements = pp.elements map { kv => ( universe.persistentStore.load(kv._1, classOf[PersistentProperty]), kv._2) }
    }
  def apply(pp: PersistentProperty): PersistentProperty.State = persistentProperties(pp)
  
  private val fp = new HashMap[PId, FunctionProperty.State]()
  lazy val functionProperties: Map[FunctionProperty, FunctionProperty.State] =
    new Map[FunctionProperty, FunctionProperty.State] {
      def get(key: FunctionProperty) = fp.get(key.pid)
      def size = fp.size
      def elements = fp.elements map { kv => ( universe.persistentStore.load(kv._1, classOf[FunctionProperty]), kv._2) }
    }
  def apply(fp: FunctionProperty): FunctionProperty.State = functionProperties(fp)
  
  def advance(advancedUniverse: Universe) = new Properties(universe)
}

object Properties extends UniverseExtensionProvider(classOf[Properties]) {
  def createExtension(universe: Universe) = new Properties(universe)
}
